import os

import dash
import pandas as pd
import plotly.express as px
from dash import dcc
from dash import html
from dash.dependencies import Input, Output

basepath = './dataset'
all_dfs = os.listdir("./dataset/fixed")
all_logs = [f.split(".")[0]+".log" for f in all_dfs]
app = dash.Dash(__name__)


app.layout = html.Div([
    dcc.RadioItems(
        id="radioitems",
        options=[{"label": x.replace("txt", "log"), "value": x} for x in all_dfs],
        value=all_dfs[0],
        labelStyle={'display': 'inline-block'}
    ),
    dcc.Graph(id="line-chart")
])


@app.callback(Output("line-chart", "figure"), [Input("radioitems", "value")])
def update_line_chart(log):
    title = log.replace("txt", "log")
    if log == "tag_B_test20211230170640.txt":
        df = pd.read_csv("./dataset/fixed/tag_B_test20211230170640.txt", delimiter=",",
                         names=["date_time", "location (dm)"])
    elif log == "tag_A_test_20211230154013.txt":
        df = pd.read_csv("./dataset/fixed/tag_A_test_20211230154013.txt", delimiter=",",
                         names=["date_time", "location (dm)"])
    else:
        df = pd.read_csv("./dataset/fixed/tag_AandB_test20211230182209.txt", delimiter=",",
                         names=["date_time", "tag", "location (dm)"])

    if log == "tag_B_test20211230170640.txt" or log == "tag_A_test_20211230154013.txt":
        fig = px.line(df, x="date_time", y="location (dm)", title=title)
    else:
        fig = px.line(df, x="date_time", y="location (dm)", color="tag", title=title)
    return fig


if __name__ == '__main__':
    app.run_server(host="0.0.0.0", port=10000, debug=True)
